﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Data.Sql;

namespace QuanLyKhoHang
{ 
    public partial class frmQuanLyNhanVien : Form
    {
        connect conn;
        public frmQuanLyNhanVien()
        {
            conn= new connect();
            InitializeComponent();

            show_hang();
        }

        public void show_hang()
        {
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = conn.getconnect();
            cmd.CommandText = "select * from nhanvien";

            DataTable dt = new DataTable();
            SqlDataReader rd;
            rd = cmd.ExecuteReader();
            dt.Load(rd);
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            txtMaNhanVien.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtTenNhanVien.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            txtTenDangNhap.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            txtMatKhau.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            txtDiaChi.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            dtpNgaySinh.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            txtGioiTinh.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
            txtQuyen.Text = dataGridView1.CurrentRow.Cells[7].Value.ToString();

            txtMaNhanVien.Enabled = false;
            txtMatKhau.Enabled = false;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmThemNhanVien frm = new frmThemNhanVien();
            frm.Show();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = conn.getconnect();
            cmd.CommandText = "update nhanvien set TenNhanVien='" + txtTenNhanVien.Text + "',DiaChi='" + txtDiaChi.Text + "',NgaySinh='" + dtpNgaySinh.Text + "',GioiTinh='" + txtGioiTinh.Text + "',quyen='" + txtQuyen.Text + "' where MaNhanVien='" + txtMaNhanVien.Text + "'";
            cmd.ExecuteNonQuery();

            MessageBox.Show("cập nhật thành công");
            show_hang();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = conn.getconnect();
            cmd.CommandText = "delete from nhanvien where MaNhanVien ='" + txtMaNhanVien.Text + "'";
            cmd.ExecuteNonQuery();
        }

        private void txtTimkiem_KeyUp(object sender, KeyEventArgs e)
        {
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = conn.getconnect();
            cmd.CommandText = "select * from nhanvien where TenNhanVien like '%" + txtTimkiem.Text + "%'";

            SqlDataReader rd;
            DataTable dt = new DataTable();
            rd = cmd.ExecuteReader();
            dt.Load(rd);
            dataGridView1.DataSource = dt;
        }
    }
}
